<?php
/**
 * 诊所管理
 * Created by PhpStorm.
 * User: dower
 * Date: 2016/7/26 0026
 * Time: 下午 15:31
 */
namespace Web\Controller\Clinic;

use Web\Model\Clinic\ClinicModel;
use Web\Model\Doctor\DoctorModel;

class ClinicController extends BaseController{

    public $model = null;
    public $model2 = null;
    private $size = 10;
    public $userinfo = null;

    public function __construct()
    {
        parent::__construct();
        $this->handleLogin(true);//该控制器所以操作均要求登录情况下
        $this->model = new ClinicModel();
        $this->model2 = new DoctorModel();
    }

    /**
     * 诊所首页
     */
    public function index(){
        //加载视图
        $this->display();
    }

    /**
     * 诊疗记录
     */
    public function treatmentlist(){
        //得到诊所的医生id
        $ids = $this->getDoctorIds();
        $where = array(
            'a.doctor_id'=>array('in',$ids)
        );
        //得到列表
        $list = $this->model2->treatmentlist($where,$this->size);
        $this->assign('size', $this->size);
        $this->assign('pageHtml', $list['pageHtml']);
        $this->assign('count', $list['count']);
        $this->assign('list', $list['rows']);
        //设置头部
        $my_header = array(
            'h_cur'=>4,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('treatment.list');
    }

    /**
     * 诊疗数据
     */
    public function treatmentcount(){
        //查询总量
        //得到所有的医生id
        $doctor_ids = $this->getDoctorIds();
        //筛选条件
        $where['doctor_id'] = array('in',$doctor_ids);
        $total = M('DoctorTreatment')->where($where)->count();
        //添加查询条件
        $date = I('get.date');
        $ids = implode(',',$doctor_ids);
        $date = $date?:date('Y-m');//默认为当前月
        $sql = "select count(*) as count from __PREFIX__doctor_treatment where `doctor_id` in (".$ids.") and DATE_FORMAT(`treat_time`, '%Y-%m')='{$date}'";
        $rst = M('DoctorTreatment')->query($sql);
        $count = 0;
        if($rst){
            $count = $rst[0]['count'];
        }
        $this->assign('date',$date);
        $this->assign('total',$total);
        $this->assign('count',$count);
        //设置头部
        $my_header = array(
            'h_cur'=>4,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('treatment.count');
    }

    /**
     * ajax获得月份
     * @param $date
     */
    public function getTreatment($date){
        $doctor_ids = $this->getDoctorIds();
        $ids = implode(',',$doctor_ids);
        //查询数量
        $date = $date?:date('Y-m');//默认为当前月
        $sql = "select a.*,b.name,c.name as doctor from __PREFIX__doctor_treatment as a join __PREFIX__patient as b on a.patient_id=b.id join __PREFIX__doctor as c on a.doctor_id=c.id where a.`doctor_id` in (".$ids.") and DATE_FORMAT(a.`treat_time`, '%Y-%m')='{$date}'";
        $rst = M('DoctorTreatment')->query($sql);
        $count = 0;
        $rows = array();
        if($rst){
            $count = count($rst);
            //处理数据
            foreach ($rst as $item){
                $temp = array();
                $temp['symptom'] = mbSub($item['symptom'],0,6);
                $temp['treatment'] = mbSub($item['treatment'],0,6);
                $temp['recipe'] = mbSub($item['recipe'],0,6);
                $temp['treat_time'] = $item['treat_time'];
                $temp['name'] = $item['name'];
                $temp['doctor'] = $item['doctor'];
                $temp['url'] = U('viewtreatment',array('id'=>$item['id']));
                $rows[] = $temp;
            }
        }
        $this->ajaxReturn(array('count'=>$count,'data'=>$rows));
    }

    /**
     * 查看诊疗详情
     * @param $id int 诊疗id
     */
    public function viewtreatment($id){
        //查询详情
        $treatment = M('DoctorTreatment')->alias('a')->field('a.*,b.clinic_name as clinic,c.name as doctor')->where(array('a.id='.$id))->join(' LEFT JOIN __DOCTOR__ c on a.doctor_id=c.id LEFT JOIN __CLINIC__ b on c.clinic_id=b.id')->find();
        $this->assign($treatment);
        //设置头部
        $my_header = array(
            'h_cur'=>0,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('treatment.detail');
    }

    /**
     * 患者评价
     */
    public function assess(){
        //得到所有的医生id
        $doctor_ids = $this->getDoctorIds();
        $where = array('a.doctor_id'=>array('in',$doctor_ids));
        //得到列表
        $list = $this->model2->assessList($where,$this->size);
        $this->assign('list', $list['rows']);
        $this->assign('pageHtml',$list['pageHtml']);
        //设置头部
        $my_header = array(
            'h_cur'=>5,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('assess.list');
    }

    /**
     * 评价数据
     */
    public function assesscount(){
        //得到所有的医生id
        $doctor_ids = $this->getDoctorIds();
        $where = array('doctor_id'=>array('in',$doctor_ids));
        //得到总数
        $total = $this->model2->assessData($where);
        //得到每月的分析数据
        $date = I('get.date');
        $date = $date?:date('Y-m');//默认为当前月
        $count = $this->model2->assessData($where,2,$date);
        $this->assign('total',$total);
        $this->assign('count',count($count));
        $this->assign('date',$date);
        //设置头部
        $my_header = array(
            'h_cur'=>5,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('assess.count');
    }

    /**
     * ajax获得月份数量
     * @param $date
     */
    public function getAssess($date){
        //得到所有的医生id
        $doctor_ids = $this->getDoctorIds();
        //筛选条件
        $where = array('doctor_id'=>array('in',$doctor_ids));
        //查询数量
        $date = $date?:date('Y-m');//默认为当前月
        $list = $this->model2->assessData($where,2,$date);
        $this->ajaxReturn(array('count'=>count($list),'data'=>$list));
    }

    /**
     * 系统消息
     */
    public function news(){
        //得到系统消息及个人消息
        $where = array('accept_id'=>array('in','0,'.UID));
        $where['accept_id'] = UID;
        $where['type'] = 2;
        $map['_complex'] = $where;
        $map['_logic'] = 'or';
        $map['accept_id'] = 0;
        $list = $this->model2->newsList($where,$this->size);
        $this->assign('list',$list['rows']);
        $this->assign('pageHtml',$list['pageHtml']);
        //设置头部
        $my_header = array(
            'h_cur'=>7
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display();
    }

    /**
     * 系统消息详情
     * @param $id
     */
    public function news_detail($id){
        //得到消息
        $info = M('SystemMessage')->find($id);
        //设置头部
        $my_header = array(
            'h_cur'=>7,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //分配其他信息
        $this->assign('info',$info);
        //加载视图
        $this->display();
    }

    /**
     * 医生管理
     */
    public function doctor(){
        //得到医生信息
        $doctors = M('Doctor')->field('id,name,good,mobile,score')->where(array('clinic_id'=>UID,'status'=>0))->order('CONVERT(name USING gbk) asc')->select();//按姓名排序
        //渲染页面
        $this->assign('list',$doctors);
        $this->assign('clinic_name',$this->userinfo['clinic_name']);
        //设置头部
        $my_header = array(
            'h_cur'=>3,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display();
    }

    /**
     * 医生主页
     * @param $id
     */
    public function doctor_detail($id){
        $info = M('Doctor')->where(array('clinic_id'=>UID,'id'=>$id))->find();
        $this->assign($info);
        //设置头部
        $my_header = array(
            'h_cur'=>3,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        $this->assign('clinic_name',$this->userinfo['clinic_name']);
        //加载视图
        $this->display();
    }

    /**
     * 添加医生
     */
    public function add_doctor(){
        if(IS_POST){
            $requestData = I('post.');
            $res = $this->model->add_doctor($requestData);
            if($res){
                $this->ajaxReturn(array('code'=>0,'url'=>U('doctor')));
            }else{
                $this->ajaxReturn(array('code'=>1,'msg'=>$this->model->getError()));
            }
        }else{
            //设置头部
            $my_header = array(
                'h_cur'=>3,
            );
            $my_header = array_merge($this->header,$my_header);
            $this->assign($my_header);
            //加载视图
            $this->display();
        }
    }

    /**
     * 删除医生
     * @param $id
     */
    public function delete_doctor($id){
        //判断医生为当前诊所的
        $rst = M('Doctor')->where(array('clinic_id'=>UID,'id'=>$id))->count();
        if(!$rst) $this->ajaxReturn(array('code'=>2,'msg'=>'网络繁忙, 请稍后再试'));
        //删除操作
        $res = $this->model->delete_doctor($id);
        if($res){
            $this->ajaxReturn(array('code'=>0,'url'=>U('doctor')));
        }else{
            $this->ajaxReturn(array('code'=>1,'msg'=>$this->model->getError()));
        }
    }

    /**
     * 患者管理
     */
    public function patient(){
        //得到诊所的医生id
        $ids = $this->getDoctorIds();
        $where = array(
            'a.doctor_id'=>array('in',$ids)
        );
        $res = $this->model->patientList($where);
        $this->assign('pageHtml', $res['pageHtml']);
        $this->assign('list', $res['rows']);
        $this->assign('count',$res['count']);
        //设置头部
        $my_header = array(
            'h_cur'=>2,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('patient');
    }

    /**
     * 患者详情
     * @param $id
     */
    public function view_patient($id){
        //得到患者信息
        $patient = M('Patient')->find($id);
        $this->assign('patient',$patient);
        //设置头部
        $my_header = array(
            'h_cur'=>2,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('patient.view');
    }

    /**
     * 患者诊疗记录
     * @param $id int
     */
    public function patient_treatmentlist($id){
        $where['a.patient_id'] = $id;
        //得到列表
        $list = $this->model2->patientTreatmentList($where);
        //处理信息
        $this->assign('count', $list['count']);
        $this->assign('list', $list['rows']);
        //得到患者信息
        $patient = M('Patient')->field('name,age,sex,remark')->where(array('id'=>$id))->find();
        $this->assign('patient',$patient);
        //设置头部
        $my_header = array(
            'h_cur'=>2,
        );
        $my_header = array_merge($this->header,$my_header);
        $this->assign($my_header);
        //加载视图
        $this->display('patient.treatmentlist');
    }

    /**
     * 得到诊所的医生id数组
     * @return array
     */
    private function getDoctorIds(){
        $doctor_ids = S('my_doctor_ids_'.UID);
        if(empty($doctor_ids)){
            $doctors = M('Doctor')->field('id')->where(array('clinic_id'=>UID,'status'=>0))->select();
            if($doctors){
                $doctor_ids = array_column($doctors,'id');
            }else{
                $doctor_ids = array(0);
            }
            S('my_doctor_ids_'.UID,$doctor_ids,C('MY_CACHE_TIME'));//缓存结果
        }
        return $doctor_ids;
    }

}